import numpy as np
import csv

from Utils import GetFeature, GetFileList

# import datetime as d


file_list, category = GetFileList.get_file_and_category('info.csv')


with open('train_data.csv', 'w') as f:
    header = []
    writer = csv.writer(f)
    for i in range(0, 7800):
        header.append("Feature" + str(i))
    header.append("Label")
    writer.writerow(header)
    header = []
    for i in range(0, len(file_list)):
        j = i + 1
        k = i + 90
        # print(j, k)
        if(j >= len(file_list)):
            j = j - len(file_list)
        if(k >= len(file_list)):
            k = k - len(file_list)
        try:
            mfcc_i = GetFeature.get_mfcc(file_list[i] + '.wav')
            mfcc_j = GetFeature.get_mfcc(file_list[j] + '.wav')
            mfcc_k = GetFeature.get_mfcc(file_list[k] + '.wav')
            X_1 = np.append(mfcc_i, mfcc_j)
            X_0 = np.append(mfcc_i, mfcc_k)
            if(category[file_list[i]] == category[file_list[j]]):
                X_1 = np.append(X_1, 1)
            else:
                X_1 = np.append(X_1, 0)
            if(category[file_list[i]] == category[file_list[k]]):
                X_0 = np.append(X_0, 1)
            else:
                X_0 = np.append(X_0, 0)
            print("y_1, y_0 is", X_1[-1], X_0[-1])
            print("X_1, X_0 shape is:", X_1.shape, X_0.shape)
            writer.writerow(X_1)
            writer.writerow(X_0)
        except Exception as e:
            print("#######################")
            print("Something Wrong, ERROR info is: ", e)
            print("#######################")



'''
############### shuffled before #################

file_list, category = GetFileList.get_file_and_category('info.csv')
stride = 10
train_list = file_list[:24000]
validate_list = file_list[24000:]

file_list = []

print(len(file_list))
print(category[train_list[200]])

train_start_time = d.datetime.now().strftime("%Y.%m.%d-%H:%M:%S")

with open('train_data100.csv', 'w') as f:
    writer = csv.writer(f)
    for i in range(0, len(train_list), stride):
        j = i + stride
        file = train_list[i:j]
        for m in range(len(file)):
            for n in range(len(file)):
                if (m != n):
                    try:
                        mfcc_m = GetFeature.get_mfcc(file[m] + '.wav')
                        mfcc_n = GetFeature.get_mfcc(file[n] + '.wav')
                        X = np.append(mfcc_m, mfcc_n)
                        X = X.flatten()
                        if (category[file[m]] == category[file[n]]):
                            y = 1
                        else:
                            y = 0
                        data_row = np.append(X, y)
                        print("lenght of data_row is :", len(data_row), "train data start time :", train_start_time)
                        writer.writerow(data_row)
                    except Exception as e:
                        print("----------------\n", e, "\n----------------")

validate_start_time = d.datetime.now().strftime("%Y.%m.%d-%H:%M:%S")

with open('validate_data100.csv', 'w') as f:
    writer = csv.writer(f)
    for i in range(0, len(validate_list), stride):
        j = i + stride
        file = validate_list[i:j]
        for m in range(len(file)):
            for n in range(len(file)):
                if (m != n):
                    try:
                        mfcc_m = GetFeature.get_mfcc(file[m] + '.wav')
                        mfcc_n = GetFeature.get_mfcc(file[n] + '.wav')
                        X = np.append(mfcc_m, mfcc_n)
                        X = X.flatten()
                        if (category[file[m]] == category[file[n]]):
                            y = 1
                        else:
                            y = 0
                        data_row = np.append(X, y)
                        print("lenght of data_row is :", len(data_row), "validate data start time: ", validate_start_time)
                        writer.writerow(data_row)
                    except Exception as e:
                        print("----------------\n", e, "\n----------------")


print("Start data-saving time is", train_start_time)

print("Stop data-saving time is", d.datetime.now().strftime("%Y.%m.%d-%H:%M:%S"))

############### shuffled before #################
'''
